package com.wowangz.cms.syscore.autotask.job;

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;

import org.quartz.JobExecutionContext;

import com.wowangz.cms.syscore.utils.JedisUtil;
import com.wowangz.cms.syscore.utils.RSAModel;
import com.wowangz.cms.syscore.utils.RSAUtils;

public class StoreLoginKeySync extends BaseJob{

	private static final int maxKeyLen=1000;
	/***********
	 * 商户端 用户登录 动态生成公钥与私钥
	 */
	public void innerExecute(JobExecutionContext context) throws Exception {
		int minKeyLen=0;
		try {
			//1.生成  公钥 与私钥
			for(minKeyLen=0;minKeyLen<maxKeyLen;minKeyLen++){
				String ming=String.valueOf(minKeyLen+1);
		        RSAModel rsa =getRSA();
		        JedisUtil.insertPublicDataObject(ming,rsa,JedisUtil.STORE_LOGINKEY);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static RSAModel getRSA() throws Exception{
		HashMap<String, Object> map;
		RSAModel rsa = new RSAModel();
		try {
			map = RSAUtils.getKeys();
			RSAPublicKey publicKey = (RSAPublicKey) map.get("public");  
			RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");  
			BigInteger public_modulus = publicKey.getModulus();//模  
			BigInteger public_exponent = publicKey.getPublicExponent();//公钥指数  
			String private_exponent = privateKey.getPrivateExponent().toString();//私钥指数  
			String publicKeyExponent = public_exponent.toString(16);//公钥指数16进制  
			String publicKeyModulus = public_modulus.toString(16);//模16进制
			rsa.setPrivate_exponent(private_exponent);
			rsa.setPublic_modulus(public_modulus.toString());
			rsa.setPublicKeyExponent(publicKeyExponent);
			rsa.setPublicKeyModulus(publicKeyModulus);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return rsa;
	}
}
